package com.zglc.ms.shop.service;

import com.zglc.ms.shop.dao.HomeTemplateDao;
import com.zglc.ms.shop.dao.HomeTemplateDetailDao;
import com.zglc.ms.shop.dao.ProductDao;
import com.zglc.ms.shop.model.ShopHomeTemplate;
import com.zglc.ms.shop.model.ShopHomeTemplateDetail;
import com.zglc.ms.shop.model.ShopProduct;

import ch.qos.logback.classic.Logger;

import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by v.sc.d on 2017/4/12.
 */
@Service
@Transactional
public class ShopHomeTemplateService {

	@Autowired
	private HomeTemplateDao homeTemplateDao;

	@Autowired
	private  ProductDao productDao;
	
	@Autowired
	private HomeTemplateDetailDao homeTemplateDetailDao;

	public List<ShopHomeTemplate> getHomeTemplateByAll() {
		//获取可显示的所有专区
		List<ShopHomeTemplate> shopHomeTemplates = homeTemplateDao.getShopHomeTemplateByAll();
		if (shopHomeTemplates == null) {
			return null;
		}
		List<ShopProduct> pros = productDao.getShopProducts();
		for (int i = 0; i < shopHomeTemplates.size(); i++) {
			//遍历获取单个专区
			ShopHomeTemplate shopHomeTemplate = shopHomeTemplates.get(i);
			
			//根据专区id获取专区内容
			List<ShopHomeTemplateDetail> homeTemplateDetails = homeTemplateDetailDao
					.getShopHomeTemplateDetailByTemplateIdAndGroup(shopHomeTemplate.getId());
			if(homeTemplateDetails == null ||homeTemplateDetails.size() == 0){
				continue;
			}
			
			boolean conFlag = false;
			for (int j = 0; j < homeTemplateDetails.size(); j++) {
				
				ShopHomeTemplateDetail shopHomeTemplateDetail = homeTemplateDetails.get(j);
				//遍历专区内容，将三合一的专列进行内容填充
				if (shopHomeTemplateDetail.getTemplateProductType() == 1) {
					List<ShopHomeTemplateDetail> subHomeTemplateDetails = homeTemplateDetailDao
							.getShopHomeTemplateDetailByTemplateIdAndGroupId(shopHomeTemplate.getId(),
									shopHomeTemplateDetail.getDetailGroupId());
					if(subHomeTemplateDetails != null ){
						if(homeTemplateDetails.size() == 1 && subHomeTemplateDetails.size() != 3){
							conFlag = true;
						}
						for(int x = 0; x < subHomeTemplateDetails.size();x++){
							ShopHomeTemplateDetail homeTemplateDetail = subHomeTemplateDetails.get(x);
							homeTemplateDetail.setShopProduct(getShopProductById(pros, homeTemplateDetail.getProductId()));
							subHomeTemplateDetails.set(x, homeTemplateDetail);
						}
						shopHomeTemplateDetail.setShopHomeTemplateDetails(subHomeTemplateDetails);
						homeTemplateDetails.set(j, shopHomeTemplateDetail);
					}
				}else{
					shopHomeTemplateDetail.setShopProduct(getShopProductById(pros, shopHomeTemplateDetail.getProductId()));
				}
			}
			if(conFlag){
				continue;
			}
			if(homeTemplateDetails == null ||homeTemplateDetails.size() == 0){
				shopHomeTemplates.remove(i);
			}else{
				shopHomeTemplate.setHomeTemplateDetails(homeTemplateDetails);
				shopHomeTemplates.set(i, shopHomeTemplate);
			}
		}
		
		shopHomeTemplates = removeNullList(shopHomeTemplates);
		
		return shopHomeTemplates;
	}
	

	public ShopProduct getShopProductById(List<ShopProduct> shopProducts, int id) {
		for(ShopProduct sp: shopProducts){
			if(sp.getId() == id){
				return sp;
			}
		}
		return null;
	}
	
	public List<ShopHomeTemplate> removeNullList(List<ShopHomeTemplate> removeShopHomeTemplates) {
		for (int i = 0; i < removeShopHomeTemplates.size(); i++) {
			ShopHomeTemplate shopHomeTemplate = removeShopHomeTemplates.get(i);
			if(ObjectUtils.equals(null, shopHomeTemplate.getHomeTemplateDetails())){
				removeShopHomeTemplates.remove(i);
				removeNullList(removeShopHomeTemplates);
			}
		}
		return removeShopHomeTemplates;
	}
	

}